<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>保存到文件</title>
 
<script type="text/javascript">
function save(){
let data = '保存的数据，可从页面获取，可从后端获取，可以是json字符串';
doSave(data , "text/latex", "hello.txt");
}

function doSave(filecontent) {

 
 filecontent = new Date()+">>>"+filecontent;
   var fso = new ActiveXObject("Scripting.FileSystemObject");  
   try{    
        file = fso.GetFile("D:\\logs\\test.txt");    
    }catch(e){    
        file = null;    
    }    
    if(!file){    
        //如果没有,创建文件    
        try{      
            fso.CreateTextFile("D:\\logs\\test.txt",true);    
            file = fso.GetFile("D:\\logs\\test.txt");    
        }catch(Ee){    
            file = null;    
        }    
    }
    if(file == null){    
        alert('创建本地文件失败,file==null');    
        return;    
    }
    ts = file.OpenAsTextStream(8,true);    
    ts.WriteLine(filecontent);    
    ts.Close();
}



</script>
</head>
<body>

<button onclick="save();">
save
</button>
<input  id="button" type="button" value="点击" onclick="save();"/>  
</body>
</html>